<?php
/**
 * @deprecated (暂时 关闭了！)
 * 发型素材的curd管理
 *
 * Author: lyun
 * Date: 2018/5/10
 * Created by Panxsoft.
 */

namespace App\Http\Controllers\Admin\Customer;

use App\Exceptions\ApiException;
use App\Http\Controllers\ApiController;
use App\Http\Middleware\CustomerAuth;
use App\Http\Requests\Admin\Customer\HairAsset\HairAssetStoreRequest;
use App\Http\Requests\Admin\Customer\HairAsset\HairAssetUpdateRequest;
use App\Models\Admin\CustomerAdmin;
use App\Models\Customer\HairAsset;
use App\Services\Utils\Guard;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class HairAssetController extends ApiController
{
    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function index(Request $request)
    {
        $this->checkCustomer();

        // 超管才能操作的
        $this->checkIsTrue(CustomerAdmin::isGuardSuperAdmin(), trans('admin.is_super_man'));

        $hair_assets = HairAsset::where('customer_id', Guard::customerAdmin()->customer_id)
            ->columnsKeywordsOrLike([
                'asset_title' => $request->keywords
            ])
            ->filterGender($request->gender)
            ->latest()
            ->paginate($request->per_page ?: 15);

        return $this->response(compact('hair_assets'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  HairAssetStoreRequest $request
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function store(HairAssetStoreRequest $request)
    {
        // 检测是否是虚拟供应商,是否开通发型试戴
        $this->checkCustomer();

        // 超管才能操作的
        $this->checkIsTrue(CustomerAdmin::isGuardSuperAdmin(), trans('admin.is_super_man'));

        $hair_asset = $this->assembleHairAsset($request);

        $stored = HairAsset::create($hair_asset);

        return $this->response(compact('stored'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  HairAssetUpdateRequest $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function update(HairAssetUpdateRequest $request, $id)
    {
        $this->checkCustomer();
        // 超管才能操作的
        $this->checkIsTrue(CustomerAdmin::isGuardSuperAdmin(),  trans('admin.is_super_man'));

        /** @var HairAsset $hair_asset */
        $hair_asset = HairAsset::where('customer_id', Guard::customerAdmin()->customer_id)
            ->find($id);
        $this->checkItemExist($hair_asset, trans('admin.hair_asset'));

        $hair_asset_data = $this->assembleHairAsset($request);

        unset($hair_asset_data['assets_dir']); //素材的目录不能修改

        $updated = $hair_asset->update($hair_asset_data);

        return $this->response(compact('updated'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function destroy($id)
    {
        $this->checkCustomer();
        // 超管才能操作的
        $this->checkIsTrue(CustomerAdmin::isGuardSuperAdmin(), trans('admin.is_super_man'));

        /** @var HairAsset $hair_asset */
        $hair_asset = HairAsset::where('customer_id', Guard::customerAdmin()->customer_id)
            ->find($id);
        $this->checkItemExist($hair_asset, trans('admin.hair_asset'));

        $deleted = $hair_asset->delete();

        return $this->response(compact('deleted'));
    }

    /**
     * @param HairAssetStoreRequest|HairAssetUpdateRequest|Request $request
     * @return array
     */
    private function assembleHairAsset(Request $request)
    {
        $data = [
            'customer_id' => Guard::customerAdmin()->customer_id,
            'sort'        => $request->sort,
            'assets_dir'  => $request->assets_dir,
            'gender'      => $request->gender,
        ];

        if ($request->hair_preview) {
            $data['hair_preview'] = $request->hair_preview;
        }

        if ($request->assets_bin) {
            $data['assets_bin'] = $request->assets_bin;
        }

        if ($request->assets_png) {
            $data['assets_png'] = $request->assets_png;
        }

        if ($request->asset_title) {
            $data['asset_title'] = $request->asset_title;
        }

        return $data;
    }

    /**
     *  检测customer是否开通了2.5d发型试戴 和 客户不是虚拟供应商
     *
     * @throws \Exception
     */
    public function checkCustomer()
    {
        $customer = Guard::customerAdmin()->customer;

        $this->checkIsTrue($customer->has_hair, trans('admin.is_has_hair'));
        $this->checkUnless($customer->isProviderTypeVirtualProvider(), trans('admin.is_virtual_provider'));
    }
}
